<?php
$COMMENT_USERS  = array();
$COMMENT_TITLES = array();
$CONTENTS		= array();
if ($comments):
	foreach ($comments->result() as $comment):
		if ($comment->content_id)
		{
			if (!isset($CONTENTS[$comment->content_id]))
			{
				$CONTENTS[$comment->content_id] = $this->Content_model->load_content(
					$comment->content_id,
					$bricabox->id
				);
			}
		}
?>

<?php
// load the user for this comment and cache them for future use
// p.s. WORST PLACE EVER for this code
if (isset($COMMENT_USERS[$comment->user_id]))
	$cmt_user = $COMMENT_USERS[$comment->user_id];
else
{
	$query 	  = $this->db->query('select * from users where id = ? limit 1', $comment->user_id);
	$cmt_user = $query->first_row();
	$COMMENT_USERS[$comment->user_id] = $cmt_user;
}

if ($is_homepage)
{
	$_cmt_title = '';
	// load the content title for this comment and cache it for future use
	// p.s. WORST PLACE EVER for this code
	if (isset($COMMENT_TITLES[$comment->content_id]))
		$_cmt_title = $COMMENT_TITLES[$comment->content_id];
	else
	{
		$query 	  	= $this->db->query('select field1_value from contents where id = ? limit 1', $comment->content_id);
		$_cmt_title = $query->first_row()->field1_value;
		$COMMENT_TITLES[$comment->content_id] = $_cmt_title;
	}
}
?>

<div class="comment" id="comment_<?php echo $comment->id; ?>">
	<p class="author">
		<a href="/member/<?php echo user_url($cmt_user); ?>"><img class="profile_avatar" src="<?php echo url_for_avatar($cmt_user->avatar, $cmt_user->id, 'user', 'small'); ?>" alt="<?php echo $cmt_user->login; ?>" width="25" height="25" /></a>
		<a href="/member/<?php echo user_url($cmt_user); ?>"><?php echo username($cmt_user); ?></a> said<?php if (@$is_homepage == true): ?> about <a href="<?php echo content_url($CONTENTS[$comment->content_id]); ?>"><?php echo $_cmt_title; ?></a><?php endif; ?>: <span class="when"><?php echo strtolower(timespan(strtotime($comment->created_at))); ?> ago</span></p>
	<div class="body">
		<?php echo auto_typography($comment->text); ?>
	</div>
</div>

<?php 
	endforeach;
else:
?>

<div>
	No comments available
</div>

<?php

endif;
?>